﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Kiiro.SiteDefinition.Core;
using Kiiro.SiteDefinition.ControlTemplates;
using PortalFileUrls = Kiiro.Common.Utility.Constants.PortalFileUrls;
using ProjectFileUrls = Kiiro.Common.Utility.Constants.ProjectFileUrls;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class Header : KiiroUserControl
    {
        public List<Control> AccessDeniedControlsToHide
        {
            get { return new List<Control> { liProfile, liPersonal, liSettings, liLogout, v2Search, v2NavigationMain, ddlProjects }; }
        }

        private Project project = new Project ( );
        public Project Project
        {
            get
            {
                if ( ( project == null || project.Title == null ) && SPHelpers.IsProject ( Base.SiteCollectionId, Base.ServerRelativeUrl ) )
                {
                    project = ProjectsModel.GetByServerRelativeWebUrl ( Base.SiteCollectionId, Base.ServerRelativeUrl );
                }
                return project;
            }
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
        }

        protected override void OnPreRender ( EventArgs e )
        {
            base.OnPreRender ( e );
            string portalWebUrl = SPHelpers.GetPortalServerRelativeUrl ( Base.SiteCollectionId, Base.ServerRelativeUrl );

            if ( Base.CurrentUser.IsFormsUser )
            {
                liLogout.Visible = Base.CurrentUser.IsFormsUser;
                liLogout.Attributes [ "class" ] = liLogout.Attributes [ "class" ] + " borderless";
            }
            else
                liHelp.Attributes [ "class" ] = liHelp.Attributes [ "class" ] + " borderless";

            LoadProjectsDropDown ( );

            lnkDashBoard.Visible = IsProject;
            lnkDashBoard.NavigateUrl = String.Format ( "{0}{1}", portalWebUrl, PortalFileUrls.PortalActivity );
            lnkMyProfile.Text = SPHelpers.ConvertHtmlToKiiroSafeText ( Base.CurrentUser.FullName );

            lnkMyProfile.NavigateUrl = String.Format ( "{0}{1}?id={2}", Base.ServerRelativeUrl, PortalFileUrls.UserDisplay, Base.CurrentUser.SPUserId );
            liProfile.Visible = Base.CurrentUser.Login != Constants.Security.AD_AUTH_USERS_DOMAIN_GROUP_NAME;

            lnkPersonalSettings.NavigateUrl = String.Format ( "{0}{1}?id={2}", Base.ServerRelativeUrl, PortalFileUrls.UserEdit, Base.CurrentUser.SPUserId );
            liPersonal.Visible =
                Base.CurrentUser.Login != Constants.Security.AD_AUTH_USERS_DOMAIN_GROUP_NAME &&
                Base.CurrentUser.Groups.Contains ( KiiroGroup.PortalMember );

            if ( ( Base.PermissionBitsSetup [ 0 ] || Base.PermissionBitsSetup [ 2 ] ) && Base.CurrentUser.IsAdministrator )
            {
                liSettings.Visible = true;
                lnkKiiroSettings.NavigateUrl = String.Format ( "{0}{1}", portalWebUrl, PortalFileUrls.KiiroEditSetup );
            }

            SetTitleAndLogo ( );
        }

        private void LoadProjectsDropDown ( )
        {
            ddlProjects.Items.Clear ( );

            List<Project> projects = ProjectsModel.GetProjectsForUser (
                Base.SiteCollectionId,
                Base.ServerRelativeUrl,
                Base.CurrentUser,
                new List<ProjectStatus> ( ) { ProjectStatus.Active } );

            projects.RemoveAll ( x => x.ServerRelativeWebUrl == Base.ServerRelativeUrl );

            ddlProjects.Items.Insert ( 0, "Go To Project →" );
            foreach ( Project project in projects )
            {
                ddlProjects.Items.Add ( new ListItem ( project.Title, project.ServerRelativeWebUrl.ToString ( ) ) );
            }

            ddlProjects.SelectedIndex = 0;
        }

        public void SetTitleAndLogo ( )
        {
            string siteTitleTemplate = "<a href='{0}{1}' class='siteNameLink'>{2}</a>";
            string siteTitle = SetupModel.GetTitle ( Base.SiteCollectionId, Base.ServerRelativeUrl );
            string siteTitleHref = Constants.PortalFileUrls.PortalActivity;

            if ( SPHelpers.IsProject ( Base.SiteCollectionId, Base.ServerRelativeUrl ) )
                siteTitleHref = Constants.ProjectFileUrls.ProjectSummary;

            litSiteTitle.Text = string.Format ( siteTitleTemplate, Base.ServerRelativeUrl, siteTitleHref, siteTitle );

            string logoUrl = SetupModel.GetLogo ( Base.SiteCollectionId, Base.ServerRelativeUrl );
            if ( !string.IsNullOrEmpty ( logoUrl ) )
            {
                imgSiteLogo.Src = logoUrl;
                logoTable.Visible = true;
            }
            else
            {
                logoTable.Visible = false;
            }
        }

        protected void btnLogout_Click ( object sender, EventArgs e )
        {
            string logoutUrl = SPHelpers.GetPortalServerRelativeUrl ( Base.SiteCollectionId, Base.ServerRelativeUrl ) + Constants.PortalFileUrls.PortalActivity;
            System.Web.Security.FormsAuthentication.SignOut ( );
            Response.Redirect ( logoutUrl );
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/Header.ascx.cs $
 * 
 * 39    1/12/10 1:19p Camerons
 * 
 * 38    1/11/10 11:51a Camerons
 * 
 * 37    1/06/10 12:39p Camerons
 * 
 * 1     1/12/09 7:48p Camerons
 * 
 * 1     25/11/09 2:56p Camerons
 * 
 * 1     18/11/09 3:28p Camerons
 * 
 * 28    18/11/09 2:48p Camerons
 * 
 * 27    28/10/09 10:32p Camerons
 * 
 * 26    10/26/09 9:25a Adamw
 * 
 * 25    10/23/09 5:38p Adamw
 * Case 348
 * 
 * 24    7/10/09 2:43p Camerons
 * 
 * 23    5/10/09 2:07p Camerons
 * 
 * 22    5/10/09 1:56p Camerons
 * Implement re-adding of the guest account code to the PortalProvisioner
 * and UserModel. Enable and disable various UI elements for guest account
 * 
 * 21    30/09/09 2:03p Camerons
 * 
 * 20    23/09/09 4:46p Camerons
 * Fixed some old code dealing with User.ManageProjects
 * 
 * 19    23/09/09 12:42p Camerons
 * 
 * 18    22/09/09 1:27p Camerons
 * 
 * 17    22/09/09 10:24a Shafia
 * Logo table is hidden when no logo is chosen.
 * 
 * 16    17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/